﻿using SD.Common;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using SD.Infrastructure.WPF.Models;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.MES.Presentation.Maps;
using SlamDunk.MES.Presentation.Models.NotifyModels;
using SlamDunk.MES.ReportView.Models;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using System.Windows;

namespace SlamDunk.MES.Client.ViewModels.StockQuery
{
    /// <summary>
    /// 选择库存弹出层-公用ViewModel
    /// </summary>
    public class SelectMaterialStockViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 库存单据服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IStockOrderContract> _iStockOrderContract;

        /// <summary>
        /// 资源定义服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IResourceContract> _iResourceContract;

        /// <summary>
        /// 基础定义服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IFoundationContract> _iFoundationContract;

        /// <summary>
        /// 库存管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IStockManageContract> _iStockManageContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public SelectMaterialStockViewModel(ServiceProxy<IStockOrderContract> iStockOrderContract, ServiceProxy<IResourceContract> iResourceContract, ServiceProxy<IFoundationContract> iFoundationContract, ServiceProxy<IStockManageContract> iStockManageContract)
        {
            this._iStockOrderContract = iStockOrderContract;
            this._iResourceContract = iResourceContract;
            this._iFoundationContract = iFoundationContract;
            this._iStockManageContract = iStockManageContract;
            //默认值
            this.PageIndex = 1;
            this.PageSize = 20;
        }

        #endregion

        #region 属性
        #region 页码 —— int PageIndex
        /// <summary>
        /// 页码
        /// </summary>
        [DependencyProperty]
        public int PageIndex { get; set; }
        #endregion

        #region 页容量 —— int PageSize
        /// <summary>
        /// 页容量
        /// </summary>
        [DependencyProperty]
        public int PageSize { get; set; }
        #endregion

        #region 总记录数 —— int RowCount
        /// <summary>
        /// 总记录数
        /// </summary>
        [DependencyProperty]
        public int RowCount { get; set; }
        #endregion

        #region 总页数 —— int PageCount
        /// <summary>
        /// 总页数
        /// </summary>
        [DependencyProperty]
        public int PageCount { get; set; }
        #endregion

        #region 关键字
        /// <summary>
        /// 关键字
        /// </summary>
        [DependencyProperty]
        public string Keywords { get; set; }
        #endregion

        #region 供应商列表
        /// <summary>
        /// 供应商列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<IdentiferNameNm> Suppliers { get; set; }
        #endregion

        #region 已选供应商
        /// <summary>
        /// 已选供应商
        /// </summary>
        [DependencyProperty]
        public IdentiferNameNm SelectedSupplier { get; set; }
        #endregion

        #region 物料库存列表
        /// <summary>
        /// 物料库存列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<Wrap<MaterialStockInfo>> MaterialStocks { get; set; }
        #endregion

        #region 工厂模型Id
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        [DependencyProperty]
        public Guid? FacilityId { get; set; }
        #endregion


        #region 物料定义Id
        /// <summary>
        /// 物料定义Id
        /// </summary>
        [DependencyProperty]
        public Guid? MaterialId { get; set; }
        #endregion

        #region 储存位置类型
        /// <summary>
        /// 储存位置类型
        /// </summary>
        [DependencyProperty]
        public StorageLocationType? StorageLocationType { get; set; }
        #endregion

        #region 储存位置Id
        /// <summary>
        /// 储存位置Id
        /// </summary>
        [DependencyProperty]
        public string StorageLocationTxt { get; set; }
        #endregion

        #region 是否多选
        /// <summary>
        /// 是否多选
        /// </summary>
        [DependencyProperty]
        public bool IsCheckbox { get; set; }
        #endregion
        #endregion

        #region 方法

        #region 加载 —— async Task Load(Guid? facilityId, Guid? materialId, StorageLocationType? storageLocationType, string storageLocationTxt,bool isCheckbox)
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="storageLocationType">储存位置类型</param>
        /// <param name="storageLocationTxt">储存位置Id</param>
        /// <param name="isCheckbox">是否多选</param>
        /// <returns></returns>
        public async Task Load(Guid? facilityId, Guid? materialId, StorageLocationType? storageLocationType, string storageLocationTxt, bool isCheckbox)
        {
            this.FacilityId = facilityId;
            this.MaterialId = materialId;
            this.StorageLocationType = storageLocationType;
            this.StorageLocationTxt = storageLocationTxt;
            this.IsCheckbox = isCheckbox;
            PageModel<EnterpriseInfo> Enterprises = await Task.Run(() =>
                      this._iResourceContract.Channel.GetEnterprisesByPage(
                         null,//关键字
                         EnterpriseType.Supplier,//工厂模型Id
                         this.FacilityId,
                         1,
                         int.MaxValue)//上级节点Id
                   );
            var EnterpriseModels = Enterprises.Datas.Select(x => x.ToIdentiferName());
            this.Suppliers = new ObservableCollection<IdentiferNameNm>(EnterpriseModels);
            await this.ReloadMaterialStock();
        }
        #endregion

        #region 搜索
        /// <summary>
        /// 搜索
        /// </summary>
        public async void LoadStockManage()
        {
            await this.ReloadMaterialStock();
        }
        #endregion

        #region 全选 —— void CheckAll()
        /// <summary>
        /// 全选
        /// </summary>
        public void CheckAll()
        {
            this.MaterialStocks.ForEach(x => x.IsChecked = true);
        }
        #endregion

        #region 取消全选 —— void UncheckAll()
        /// <summary>
        /// 取消全选
        /// </summary>
        public void UncheckAll()
        {
            this.MaterialStocks.ForEach(x => x.IsChecked = false);
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证
            MaterialStockInfo[] checkedRoles = this.MaterialStocks.Where(x => x.IsChecked == true).Select(x => x.Model).ToArray();
            if (!this.IsCheckbox)
            {
                if (!checkedRoles.Any() || checkedRoles.Count() != 1)
                {
                    MessageBox.Show("请勾选一条库存单据！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            else
            {
                if (!checkedRoles.Any())
                {
                    MessageBox.Show("请至少勾选一条库存单据！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            #endregion
            await base.TryCloseAsync(true);
        }
        #endregion

        //private

        #region 加载取物料库存列表 —— async Task ReloadMaterialStock()
        /// <summary>
        /// 加载取物料库存列表
        /// </summary>
        private async Task ReloadMaterialStock()
        {
            this.Busy();
            PageModel<MaterialStockInfo> MaterialStockList = await Task.Run(() => this._iStockManageContract.Channel.GetMaterialStocksByPage(
                 this.Keywords,//关键字
                 this.MaterialId,//物料定义Id
                 null,//物料批次Id
                 this.SelectedSupplier?.Id,//供应商Id
                 this.StorageLocationType,//储存位置类型
                 this.StorageLocationTxt,//储存位置Id
                 this.FacilityId,//工厂模型Id
                 1,//页码
                 int.MaxValue));
            this.RowCount = MaterialStockList.RowCount;
            this.PageCount = MaterialStockList.PageCount;
            this.MaterialStocks = new ObservableCollection<Wrap<MaterialStockInfo>>(MaterialStockList.Datas.Select(x => x.Wrap()));

            this.Idle();
        }
        #endregion
        #endregion
    }
}
